package com.yzk.aigy.controller;

import com.yzk.aigy.common.Result;
import com.yzk.aigy.model.system.SysMenu;
import com.yzk.aigy.model.vo.AssginMenuVo;
import com.yzk.aigy.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理控制器")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @ApiOperation("菜单列表")
    @GetMapping("/findNodes")
    public Result<List<SysMenu>> findNodes() {
       return Result.ok(sysMenuService.findNodes());
    }

    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public Result<Object> addMenu(@RequestBody SysMenu sysMenu){
        return Result.ok(sysMenuService.save(sysMenu));
    }

    @ApiOperation("根据ID查询菜单")
    @GetMapping("/findNodeById/{id}")
    public Result<SysMenu> findNodeById(@PathVariable Long id){
        return Result.ok(sysMenuService.getById(id));
    }

    @ApiOperation("修改菜单")
    @PostMapping("/updateMenu")
    public Result<Object> updateById(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/removeMenu/{id}")
    public Result<Object> removeMenu(@PathVariable Long id) {
        sysMenuService.removeMenuById(id);
        return Result.ok();
    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result<List<SysMenu>> toAssign(@PathVariable Long roleId){
        return Result.ok(sysMenuService.findSysMenuByRoleId(roleId));
    }
    @ApiOperation("给角色分配权限")
    @PostMapping("/doAssign")
    public Result<Object> doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }

}
